package com.book.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @Author LiJinCheng
 * @Date 2020/11/13 1:49 下午
 * @Version 1.0
 * @package com.book.util
 **/
@Component
public class RedisUtil {
    @Autowired
    private Jedis jedis;

    public boolean exists(String key) {
        return jedis.exists(key);
    }
    public boolean exists(byte[] key){
        return jedis.exists(key);
    }
    /**
     * 设置字符串
     */
    public boolean set(final String key,String value){
        try{
            jedis.set(key,value);
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    public boolean set(final String key,Object o){
        try{
            jedis.set(key.getBytes(), SerializeUtil.serizlize(o));
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }
    /**
     * 获取
     */
    public String get(String key){
        return jedis.get(key);
    }
    public Object get(byte[] key){
        byte[] bytes = jedis.get(key);
        return SerializeUtil.deserialize(bytes);
    }

    /**
     * 列表
     */
    public boolean rPush(String key,Object o){
        try{
            jedis.rpush(key.getBytes(),SerializeUtil.serizlize(o));
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    public boolean rPush(String key,String value){
        try{
            jedis.rpush(key,value);
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    public List<?> lRange(String key, long l, long l1, Class<?> objectClass){
        ObjectMapper objectMapper = new ObjectMapper();
        List<byte[]> lrange = jedis.lrange(key.getBytes(), l, l1);
        List<Object> list = new ArrayList<>();
        for (byte[] b:lrange
             ) {
            Object o1 = objectMapper.convertValue(SerializeUtil.deserialize(b), objectClass);
            list.add(o1);
        }

        return list;
    }

    public List<String> lRangeS(String key,long l, long l1){
        return jedis.lrange(key,l,l1);
    }

    /**
     * 集合
     */
    public Boolean sAdd(String key,String value){
        try{
            jedis.sadd(key,value);
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 判断元素是否存在
     * @param key
     * @return
     */
    public Set<String> sMembers(String key){
        return jedis.smembers(key);
    }

    /**
     * 返回集合中全部元素
     * @param key
     * @param value
     * @return
     */
    public Boolean sIsMember(String key,String value){
        return jedis.sismember(key,value);
    }

    /**
     * 移除并返回一个随机元素
     * @param key
     * @return
     */
    public String sPop(String key){
        return jedis.spop(key);
    }

    public Boolean swapSetValue(String key,String newValue,String oldValue){
        Long srem = jedis.srem(key, oldValue);
        if (srem > 0){
            Long sadd = jedis.sadd(key, newValue);
            return sadd > 0;
        }
        return false;
    }

    public Boolean delRedisKey(String key){
        Long del = jedis.del(key);
        return del > 0;
    }
}
